using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using BONSAI.Common;
using BONSAI.Common.DTO;

namespace BONSAI.DAL
{
	public class PaymentsDAO
	{
		public const string PAYMENTS_GET_BY_ID = "PaymentsGetByID";
		public const string PAYMENTS_GET_ALL = "PaymentsGetAll";
		public const string PAYMENTS_INSERT = "PaymentsInsert";
		public const string PAYMENTS_UPDATE_BY_ID = "PaymentsUpdateByID";
		public const string PAYMENTS_DELETE_BY_ID = "PaymentsDeleteByID";

		public const string PAYMENTSID = "PaymentsID";
		public const string NAMEFULL = "NameFull";
		public const string ACCOUNT = "Account";
		public const string DESCRIPTION = "Description";
		public const string ISBANK = "IsBank";
		public const string POSITION = "Position";
		public const string CREATEDATE = "Createdate";

		#region region properties
		public Database Database { get; set; }
		#endregion

		#region Constructor
		public PaymentsDAO()
		{
			Database = DatabaseFactory.CreateDatabase(BONSAIConstants.BONSAI_CONNECTION_STRING);
		}
		#endregion


		public PaymentsDTO GetByID(string id)
		{
			var command = Database.GetStoredProcCommand(PAYMENTS_GET_BY_ID);
			try
			{
				Database.AddInParameter(command, PAYMENTSID, DbType.String, id);
				var dataReader = Database.ExecuteReader(command);
				while (dataReader.Read())
				{
					var item = GetPaymentsFromDataReader(dataReader);
					return item;
				}
			}
			catch (Exception ex)
			{
				throw new Exception(ex.ToString());
			}
			return null;
		}


		public IList<PaymentsDTO> GetAll()
		{
			var command = Database.GetStoredProcCommand(PAYMENTS_GET_ALL);
			try
			{
				var dataReader = Database.ExecuteReader(command);
				var lstItem = new List<PaymentsDTO>();
				while (dataReader.Read())
				{
					var item = GetPaymentsFromDataReader(dataReader);
					lstItem.Add(item);
				}
				return lstItem;
			}
			catch (Exception ex)
			{
				throw new Exception(ex.ToString());
			}
			return null;
		}


		public string Insert(PaymentsDTO payments)
		{
			var command = Database.GetStoredProcCommand(PAYMENTS_INSERT);
			try
			{
				Database.AddOutParameter(command, PAYMENTSID, DbType.String, 36);
				Database.AddInParameter(command, NAMEFULL, DbType.String, payments.NameFull);
				Database.AddInParameter(command, ACCOUNT, DbType.String, payments.Account);
				Database.AddInParameter(command, DESCRIPTION, DbType.String, payments.Description);
				Database.AddInParameter(command, ISBANK, DbType.String, payments.IsBank);
				Database.AddInParameter(command, POSITION, DbType.Int32, payments.Position);
				Database.AddInParameter(command, CREATEDATE, DbType.DateTime, payments.Createdate);
				var dataReader = Database.ExecuteReader(command);
				return Database.GetParameterValue(command, PAYMENTSID).ToString();
			}
			catch (Exception ex)
			{
				throw new Exception(ex.ToString());
			}
			return null;
		}


		public void UpdateByID(PaymentsDTO payments)
		{
			var command = Database.GetStoredProcCommand(PAYMENTS_UPDATE_BY_ID);
			try
			{
				Database.AddInParameter(command, PAYMENTSID, DbType.String, payments.PaymentsID);
				Database.AddInParameter(command, NAMEFULL, DbType.String, payments.NameFull);
				Database.AddInParameter(command, ACCOUNT, DbType.String, payments.Account);
				Database.AddInParameter(command, DESCRIPTION, DbType.String, payments.Description);
				Database.AddInParameter(command, ISBANK, DbType.String, payments.IsBank);
                Database.AddInParameter(command, POSITION, DbType.Int32, payments.Position);
				Database.AddInParameter(command, CREATEDATE, DbType.DateTime, payments.Createdate);
				var dataReader = Database.ExecuteReader(command);
			}
			catch (Exception ex)
			{
				throw new Exception(ex.ToString());
			}
		}


		public void DeleteByID(string paymentsid)
		{
			var command = Database.GetStoredProcCommand(PAYMENTS_DELETE_BY_ID);
			try
			{
				Database.AddInParameter(command, PAYMENTSID, DbType.String, paymentsid);
				var dataReader = Database.ExecuteReader(command);
			}
			catch (Exception ex)
			{
				throw new Exception(ex.ToString());
			}
		}


		private static PaymentsDTO GetPaymentsFromDataReader(IDataRecord dataReader)
		{
			var item = new PaymentsDTO();
			try
			{

				item.PaymentsID = (string.IsNullOrEmpty(Convert.ToString(dataReader[PAYMENTSID]))) ? "" : Convert.ToString(dataReader[PAYMENTSID]);
				item.NameFull = (string.IsNullOrEmpty(Convert.ToString(dataReader[NAMEFULL]))) ? "" : Convert.ToString(dataReader[NAMEFULL]);
				item.Account = (string.IsNullOrEmpty(Convert.ToString(dataReader[ACCOUNT]))) ? "" : Convert.ToString(dataReader[ACCOUNT]);
				item.Description = (string.IsNullOrEmpty(Convert.ToString(dataReader[DESCRIPTION]))) ? "" : Convert.ToString(dataReader[DESCRIPTION]);
				item.IsBank = (string.IsNullOrEmpty(Convert.ToString(dataReader[ISBANK]))) ? 0 : Convert.ToInt32(dataReader[ISBANK].ToString());
				item.Position = (string.IsNullOrEmpty(Convert.ToString(dataReader[POSITION]))) ? 0 : Convert.ToInt32(dataReader[POSITION].ToString());
				item.Createdate = (string.IsNullOrEmpty(Convert.ToString(dataReader[CREATEDATE]))) ? DateTime.MinValue : DateTime.Parse(dataReader[CREATEDATE].ToString());
			}
			catch (Exception ex)
			{
				throw new Exception(ex.ToString());
			}
			return item;
		}
	}
}

